package ch.usi.inf.pf2.alu.nodes;

import ch.usi.inf.pf2.cell.Data;
import ch.usi.inf.pf2.cell.Value;

/**
 * 
 * @author Yotam Sharon
 * 
 */

public abstract class ArithmeticOperationNode implements Node {

	private final Node left;
	private final Node right;
	private boolean isZeroNode;

	public ArithmeticOperationNode(final Node left, final Node right) {
		this.left = left;
		this.right = right;
		isZeroNode = false;
	}

	public final Data evaluate() {
		if (left instanceof ZeroNode || right instanceof ZeroNode) {
			isZeroNode = true;
		}
		final Data leftValue = left.evaluate();
		final Data rightValue = right.evaluate();
		if (leftValue instanceof Value && rightValue instanceof Value) {
			final Value leftNumericValue = (Value) leftValue;
			final double leftNumber = leftNumericValue.getInstanceDouble();
			final Value rightNumericValue = (Value) rightValue;
			final double rightNumber = rightNumericValue.getInstanceDouble();
			return new Value(compute(leftNumber, rightNumber), isZeroNode);
		} else {
			return null;
		}
	}

	protected abstract double compute(double left, double right);
}
